package cn.tedu.fruitshop.service.impl;

import cn.tedu.fruitshop.ex.ServiceException;
import cn.tedu.fruitshop.mapper.AddressMapper;
import cn.tedu.fruitshop.pojo.dto.AddressAddNewDTO;
import cn.tedu.fruitshop.pojo.entity.Address;
import cn.tedu.fruitshop.pojo.entity.Item;
import cn.tedu.fruitshop.pojo.vo.AddressListItemVO;
import cn.tedu.fruitshop.pojo.vo.AddressStandardVO;
import cn.tedu.fruitshop.pojo.vo.ItemStandardVO;
import cn.tedu.fruitshop.service.AddressService;
import cn.tedu.fruitshop.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 地址业务的实现类
 * @author www
 * @create 2022-11-11 21:43
 */
@Slf4j
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    AddressMapper addressMapper;

    @Override
    public void add(AddressAddNewDTO addressAddNewDTO) {
        log.debug("开始执行添加地址的操作，参数:{}",addressAddNewDTO);
        log.debug("查询是否存在这个地址");
        String location = addressAddNewDTO.getLocation();
        Integer userId = addressAddNewDTO.getUserId();
        int rows = addressMapper.getStandardByLocation(location,userId);
        if (rows != 0){
            String message = "该地址已经存在，添加失败";
            log.debug("{}",message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        Address address = new Address();
        BeanUtils.copyProperties(addressAddNewDTO,address);
        address.setIsDefault(0);
        log.debug("不存在，开始添加，{}",address);
        rows = addressMapper.insert(address);
        if (rows != 1){
            String message = "添加失败,服务器繁忙，请稍后再试";
            log.debug("{}",message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void deleteByLocation(String location) {
        log.debug("开始执行【根据title删除地址】的操作，参数:{}",location);
        log.debug("查询是否存在这个地址");
        /*int rows = addressMapper.getStandardByLocation(location);
        if (0 == rows){
            String message = "该地址不存在，删除失败";
            log.debug("{}",message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }*/
        log.debug("存在，开始删除，{}",location);
        int rows = addressMapper.deleteByLocation(location);
        if (1 != rows){
            String message = "删除失败,服务器繁忙，请稍后再试";
            log.debug("{}",message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public AddressStandardVO selectById(Integer id) {
        log.debug("开始处理根据id查询地址详情的业务,参数为:{}",id);
        AddressStandardVO resultById = addressMapper.getStandardById(id);
        if (resultById == null){
            String message = "查询详情失败,服务器繁忙，请稍后再试";
            log.debug("{}",message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        log.debug("查询结果为:{}",resultById);

        return resultById;
    }

    @Override
    public List<AddressListItemVO> list(Integer id) {
        log.debug("开始查询地址列表：无参");
        List<AddressListItemVO> list = addressMapper.list(id);
        return list;
    }

    @Override
    public void updateAddress(AddressAddNewDTO addressAddNewDTO) {
        Address address = new Address();
        BeanUtils.copyProperties(addressAddNewDTO,address);
        int update = addressMapper.update(address);
        if(update==0){
            String message = "修改失败";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    @Override
    public void setEnable(Integer id) {
        updateEnableById(id, 1);
    }

    @Override
    public void setDisable(Integer id) {
        updateEnableById(id, 0);
    }

    @Override
    public void deleteById(Integer id) {
        int row = addressMapper.deleteById(id);
        if (row == 0){
            String message = "删除地址数据失败，服务器网络波动！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    private void updateEnableById(Integer id, Integer enable) {
        String[] tips = {"非默认", "默认"};
        log.debug("开始处理【{}地址】的业务，参数：{}", tips[enable], id);
        // 调用Mapper对象的getDetailsById()方法执行查询
        AddressStandardVO resultById = addressMapper.getStandardById(id);
        // 判断查询结果是否为null
        if (resultById == null) {
            String message = tips[enable] + "查询地址数据失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 判断查询结果中的enable是否为1
        if (resultById.getIsDefault().equals(enable)) {
            String message = tips[enable] + "修改地址状态失败，当前地址已经处于" + tips[enable] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        // 准备执行更新
        Address address = new Address();
        address.setId(id);
        address.setIsDefault(enable);
        int rows = addressMapper.update(address);
        if (rows != 1) {
            String message = tips[enable] + "修改地址状态失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }

    }

}
